package com.leyou.item.controller;

import com.leyou.common.constants.LyConstants;
import com.leyou.item.pojo.Category;
import com.leyou.item.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 分类Controller
 */
@RestController
//@CrossOrigin
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 根据父id查询分类信息
     */
    @GetMapping("/category/of/parent")
    public ResponseEntity<List<Category>> findCategoriesByPId(@RequestParam("pid") Long pid) {
        List<Category> categories = categoryService.findCategoriesByPId(pid);
        return ResponseEntity.ok(categories);
    }

    /**
     * 根据分类id集合 查询 分类对象集合
     */
    @GetMapping("/category/list")
    public ResponseEntity<List<Category>> findCategoriesByIds(@RequestParam("ids") List<Long> ids) {
        List<Category> categories = categoryService.findCategoriesByIds(ids);
        return ResponseEntity.ok(categories);
    }

    /**
     * 查询所有分类
     */
    @GetMapping("/category/findAll")
    public ResponseEntity<Map<String,List<Category>>> findAllCategory() {
        Map<String, List<Category>> map = categoryService.findAllCategory();
        return ResponseEntity.ok(map);
    }

}
